package com.hangzhou.xyj.ui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.ui.dialog.base.BaseDialog;


/**
 * Created by neo.duan on 2015/10/8.
 * 公共提示对话框
 */
public class ProgressDialog extends BaseDialog {
    private boolean hasNegative;
    private boolean hasTitle;
    private ProgressBar mProgressBar;

    /**
     * 构造函数
     *
     * @param context
     */
    public ProgressDialog(Context context, String title, String message, String buttonText, boolean hasNegative, boolean hasTitle) {
        super(context);
        super.setMessageLine1(message);
        super.setNamePositiveButton(buttonText);
        this.hasNegative = hasNegative;
        this.hasTitle = hasTitle;
        super.setTitle(title);
    }

    public ProgressDialog(Context context, String title, String message, String buttonText, String negetiveText, boolean isCancel) {
        super(context);
        super.setMessageLine1(message);
        super.setNamePositiveButton(buttonText);
        this.hasNegative = false;
        super.setNameNegativeButton(negetiveText);
        this.hasTitle = true;
        super.setTitle(title);
        super.setCancel(isCancel);
    }

    /**
     * 分享成功之后调用的对话框，没有标题
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/31 10:45
     */
    public ProgressDialog(Context context, String messageLine1, String messageLine2, String positiveStr, String negativeStr) {
        super(context);
        this.hasTitle = false;
        super.setMessageLine1(messageLine1);
        super.setMessageLine2(messageLine2);
        super.setNamePositiveButton(positiveStr);
        super.setNameNegativeButton(negativeStr);
    }

    /**
     * 默认title
     *
     * @param context
     * @param message
     * @param buttonText
     * @param negetiveText
     * @param isCancel
     */
    public ProgressDialog(Context context, String message, String buttonText, String negetiveText, boolean isCancel) {
        super(context);
        super.setMessageLine1(message);
        super.setNamePositiveButton(buttonText);
        this.hasNegative = false;
        super.setNameNegativeButton(negetiveText);
        this.hasTitle = true;
        super.setTitle(context.getString(R.string.default_tips_title));
        super.setCancel(isCancel);
    }

    /**
     * 默认title <br/>
     * 可以在区域外取消对话框
     *
     * @param context
     * @param message
     * @param buttonText
     * @param negetiveText
     */
    public ProgressDialog(Context context, String message, String buttonText, String negetiveText) {
        super(context);
        super.setMessageLine1(message);
        super.setNamePositiveButton(buttonText);
        this.hasNegative = false;
        super.setNameNegativeButton(negetiveText);
        this.hasTitle = true;
        super.setTitle(context.getString(R.string.default_tips_title));
        super.setCancel(true);
    }

    /**
     * @param context
     * @param message
     * @param buttonText
     */
    public ProgressDialog(Context context, String message, String buttonText) {
        super(context);
        super.setMessageLine1(message);
        super.setNamePositiveButton(buttonText);
        this.hasNegative = false;
        this.hasTitle = true;
        super.setTitle(message);
        super.setCancel(false);
    }

    private void initView() {
        setView(View.inflate(mContext, R.layout.layout_progress_dialog, null));
        mProgressBar = findView(R.id.dialog_progress_bar);
    }

    /**
     * 创建对话框
     */
    @Override
    protected void onBuilding() {
        super.setWidth(dip2px(mContext, 300));
        if (hasNegative) {
            super.setNameNegativeButton("取消");
        }
        if (!hasTitle) {
            super.setHasTitle(false);
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    /**
     * 设置进度
     * @param progress
     */
    public void setProgress(int progress) {
        mProgressBar.setProgress(progress);
    }

    public int dip2px(Context context, float dipValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (scale * dipValue + 0.5f);
    }

    @Override
    protected void onDismiss() {

    }

    @Override
    protected void OnClickNegativeButton() {
        if (onCancelListener != null) {
            onCancelListener.onClick(this, 0);
        }
    }

    /**
     * 确认按钮，触发onSuccessListener的onClick
     */
    @Override
    protected boolean OnClickPositiveButton() {
        if (onSuccessListener != null) {
            onSuccessListener.onClick(this, 1);
        }
        return true;
    }
}
